package com.jltx.xq.simplewatch.news.model.impl;

import com.jltx.xq.simplewatch.db.helper.NewsRecordDBHelper;
import com.jltx.xq.simplewatch.db.helper.NewsStateDBHelper;
import com.jltx.xq.simplewatch.news.api.WeChatApiService;
import com.jltx.xq.simplewatch.news.model.IWeChatNetNewsModel;
import com.jltx.xq.simplewatch.news.model.IWeChatNewsModel;
import com.jltx.xq.simplewatch.news.model.bean.NewsState;
import com.jltx.xq.simplewatch.news.model.bean.WeChatNews;
import com.jltx.xq.simplewatch.news.model.bean.WeChatResponse;
import com.jltx.xq.simplewatch.utils.GsonFactoryUtils;

import java.util.ArrayList;
import java.util.List;

import cn.jltx.utils.XqLog;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;



/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/25.
 */

public class WeChatNewsNetModelImpl implements IWeChatNewsModel,IWeChatNetNewsModel {

    private Integer lastMaxPageNewsRecord = null;  //缓存当前最大的页码

    @Override
    public Observable<List<WeChatNews>> getWeChatNews(String channel, int limitNum,boolean isDown) {
        return WeChatApiService.getInstance().getWeChatApi().getWeChatNews(limitNum,getLastMaxPageNewsRecord()+1)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .map(new Function<WeChatResponse, List<WeChatNews>>() {
                    @Override
                    public List<WeChatNews> apply(@NonNull WeChatResponse weChatResponse) throws Exception {
                        XqLog.d(WeChatNewsModelImpl.TAG,"网络过来的新闻列表"+Thread.currentThread());
                        List<WeChatNews> weChatNewsList =  null;
                        if(weChatResponse.getCode() == 200){
                            weChatNewsList = weChatResponse.getNewslist();  //获取网络过来的新闻列表
                            if(weChatNewsList != null){
                                WeChatNews weChatNews;
                                for (int i=0; i < weChatNewsList.size(); i++){  //依次通过channel和对应的新闻bean的url做为key去获取新闻已读和删除的状态
                                    weChatNews = weChatNewsList.get(i);
                                    NewsState newsState = NewsStateDBHelper.getNewsState(channel,weChatNews.getUrl());  //通过channel和对应的新闻bean的url做为key去获取新闻已读和删除的状态
                                    if(newsState != null){
                                        weChatNews.setIsRead(newsState.getIs_read()); //设置到已经存在的新闻bean里面
                                        weChatNews.setIsDel(newsState.getIs_del());
                                    }
                                }
                            }
                        }
                        return weChatNewsList != null ? weChatNewsList : new ArrayList<WeChatNews>();
                    }
                });
    }

    @Override
    public  void saveWeChatNews(final String channel, final int page, final List<WeChatNews> newsList) {
        //将最新页码的新闻列表保存到数据库
        NewsRecordDBHelper.saveNews(channel,page,(int)System.currentTimeMillis(), GsonFactoryUtils.providerGson().toJson(newsList));
    }

    @Override
    public boolean isHadCacheNewsRecord() {
        return lastMaxPageNewsRecord != null;
    }

    public Integer getLastMaxPageNewsRecord() {
        return lastMaxPageNewsRecord == null ? 0 : lastMaxPageNewsRecord;
    }

    public void setLastMaxPageNewsRecord(Integer lastMaxPageNewsRecord) {
        this.lastMaxPageNewsRecord = lastMaxPageNewsRecord;
    }
}
